package com.dai.zhenfujin.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.BaseAdapter.BaseRecycleAdapter;
import com.dai.zhenfujin.BaseAdapter.BaseViewHolder;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.bean.CommentListBean;
import com.dai.zhenfujin.utils.UIUtils;
import com.dai.zhenfujin.visitingcard.activity.CommentDetailsActivity;
import com.dai.zhenfujin.visitingcard.app.PersonalDetailsActivity;
import com.dai.zhenfujin.visitingcard.app.PersonalDetailsCommentActivity;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.dai.zhenfujin.utils.UIUtils.getResources;

public class PersonalCommentAdapter extends BaseRecycleAdapter<CommentListBean.ComListBean> {
    public CommentDetailsActivity context;
    public PersonalDetailsCommentActivity context2;
    public boolean flag;
    private CommentListBean.ComListBean comListBean;
    private EditText et_send_comment;
    public String user_id;
    public static InputMethodManager inputMethodManager;

    public PersonalCommentAdapter(Context context, List<CommentListBean.ComListBean> dataList, EditText et_send_comment) {
        super(context, dataList, R.layout.item_people__comment);
        if (context instanceof CommentDetailsActivity) {
            this.context = (CommentDetailsActivity) context;
            this.et_send_comment = et_send_comment;
        } else if (context instanceof PersonalDetailsCommentActivity) {
            this.context2 = (PersonalDetailsCommentActivity) context;
            this.et_send_comment = et_send_comment;
        }
    }

    @Override
    public void bindData(BaseViewHolder holder,final CommentListBean.ComListBean data) {
        comListBean = data;
        holder.setText(R.id.tv_name, data.getUser_name());
        holder.setText(R.id.tv_time, data.create_time.substring(0, data.create_time.length() - 2));

        holder.setText(R.id.tv_connect, data.getCom_content());  //civ_comment_people_head_photo

        holder.getView(R.id.civ_comment_people_head_photo).setOnClickListener(v -> {
            if (null != context){
                context.startActivity(new Intent(context, PersonalDetailsActivity.class)
                        .putExtra("user_id", data.getUser_id()));
            }else {
                context2.startActivity(new Intent(context2, PersonalDetailsActivity.class)
                        .putExtra("user_id", data.getUser_id()));
            }
        });


        if (!TextUtils.isEmpty(data.getP_user_name())){
            String connect =  "回复" + data.getP_user_name() + ":" + data.getCom_content();
            SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(connect);
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(R.color.yellow));
            spannableBuilder.setSpan(colorSpan, 2, 2 + data.getP_user_name().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            holder.setText(R.id.tv_connect,  spannableBuilder);
        }else {
            holder.setText(R.id.tv_connect, data.getCom_content());
        }

        if (null != context) {
            Glide.with(context).load(data.getUser_logo()).into((CircleImageView) holder.getView(R.id.civ_comment_people_head_photo));
        }else {
            Glide.with(context2).load(data.getUser_logo()).into((CircleImageView) holder.getView(R.id.civ_comment_people_head_photo));
        }

        holder.getView(R.id.tv_huifu).setOnClickListener(v -> {
            user_id = data.getUser_id();
            flag = true;
          //  showSoftInputFromWindow(et_send_comment);
            Handler handle = new Handler();
                handle.postDelayed(() -> {
                if (null != context) {
                   // InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                   // inputMethodManager.showSoftInput(et_send_comment, 0);

                   // et_send_comment.requestFocus();
                 //   InputMethodManager manager = ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE));
                   // if (manager != null) manager.showSoftInput(v, 0);

                    context.inputMethodManager.showSoftInput(et_send_comment, 0);
                    et_send_comment.setHint("回复" + data.getUser_name());

                  //  et_send_comment.setHint("回复" + data.getUser_name());
                }else {
                   // InputMethodManager inputMethodManager = (InputMethodManager) et_send_comment.getContext().getSystemService(context2.INPUT_METHOD_SERVICE);
                    context2.inputMethodManager.showSoftInput(et_send_comment, 0);
                    et_send_comment.setHint("回复" + data.getUser_name());
                }
            }, 0);
        });
    }

    private void showSoftInputFromWindow(EditText et_send_comment) {
        et_send_comment.setFocusable(true);
        et_send_comment.setFocusableInTouchMode(true);
        et_send_comment.requestFocus();
           context2.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
